package error

import "sort"

//思路：相当于两数之和是否等于-c，考虑采用双指针 【不能合理判断是否重复，并且忽略了三个0的存在】
func threeSum(nums []int) [][]int {
	result := make([][]int, 0)
	//数组进行排序
	sort.Ints(nums)
	gtZero := -1
	tempMap := make(map[int]int)
	for index, num := range nums {
		if num > 0 {
			tempMap[num] = index
			if gtZero == -1 {
				gtZero = index
			}
		}
	}

	for index, _ := range tempMap {
		left, right := 0, gtZero-1
		for left < right {
			if sum := nums[left] + nums[right]; sum+index == 0 {
				result = append(result, []int{nums[left], nums[right], index})
				left++
				right--
			} else if sum+index < 0 {
				left++
			} else if sum+index > 0 {
				right--
			}
		}
	}
	return result
}
